package com.ruoyi.production.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.production.domain.Flow;
import com.ruoyi.production.mapper.CircuitMapper;
import com.ruoyi.production.domain.Circuit;
import com.ruoyi.production.service.ICircuitService;

/**
 * 产品工艺线路Service业务层处理
 * 
 * @author admin
 * @date 2024-08-02
 */
@Service
public class CircuitServiceImpl implements ICircuitService 
{
    @Autowired
    private CircuitMapper circuitMapper;

    /**
     * 查询产品工艺线路
     * 
     * @param circuitId 产品工艺线路主键
     * @return 产品工艺线路
     */
    @Override
    public Circuit selectCircuitByCircuitId(Long circuitId)
    {
        return circuitMapper.selectCircuitByCircuitId(circuitId);
    }

    /**
     * 查询产品工艺线路列表
     * 
     * @param circuit 产品工艺线路
     * @return 产品工艺线路
     */
    @Override
    public List<Circuit> selectCircuitList(Circuit circuit)
    {
        return circuitMapper.selectCircuitList(circuit);
    }

    /**
     * 新增产品工艺线路
     * 
     * @param circuit 产品工艺线路
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCircuit(Circuit circuit)
    {
        circuit.setCreateTime(DateUtils.getNowDate());
        int rows = circuitMapper.insertCircuit(circuit);
        insertFlow(circuit);
        return rows;
    }

    /**
     * 修改产品工艺线路
     * 
     * @param circuit 产品工艺线路
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCircuit(Circuit circuit)
    {
        circuitMapper.deleteFlowByCircuitId(circuit.getCircuitId());
        insertFlow(circuit);
        return circuitMapper.updateCircuit(circuit);
    }

    /**
     * 批量删除产品工艺线路
     * 
     * @param circuitIds 需要删除的产品工艺线路主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCircuitByCircuitIds(Long[] circuitIds)
    {
        circuitMapper.deleteFlowByCircuitIds(circuitIds);
        return circuitMapper.deleteCircuitByCircuitIds(circuitIds);
    }

    /**
     * 删除产品工艺线路信息
     * 
     * @param circuitId 产品工艺线路主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCircuitByCircuitId(Long circuitId)
    {
        circuitMapper.deleteFlowByCircuitId(circuitId);
        return circuitMapper.deleteCircuitByCircuitId(circuitId);
    }

    /**
     * 新增产品工艺流程信息
     * 
     * @param circuit 产品工艺线路对象
     */
    public void insertFlow(Circuit circuit)
    {
        List<Flow> flowList = circuit.getFlowList();
        Long circuitId = circuit.getCircuitId();
        if (StringUtils.isNotNull(flowList))
        {
            List<Flow> list = new ArrayList<Flow>();
            for (Flow flow : flowList)
            {
                flow.setCircuitId(circuitId);
                list.add(flow);
            }
            if (list.size() > 0)
            {
                circuitMapper.batchFlow(list);
            }
        }
    }
}
